<template>
  <div class="login">
    <div class="left">
      <div class="header">
        <img class="logo" src="@/assets/img/logo.png" alt="logo" />
        <span>黑马面面</span>
        <span class="line"></span>
        <span>用户登录</span>
      </div>
      <el-form
        class="form"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="0px"
      >
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="17">
              <el-input
                v-model="form.code"
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
              ></el-input>
            </el-col>
            <el-col :span="7"
              ><img
                :src="imageCodeUrl"
                @click="updateImgUrl"
                alt=""
                class="img-code" /></el-col
          ></el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass"> </el-checkbox>
          <span class="pass-protocol">
            我已阅读并同意<el-link type="primary">用户协议</el-link>和<el-link
              type="primary"
              >隐私条款</el-link
            ></span
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="login">登 录</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="showRegister"
            >注 册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/img/login_banner_ele.png" alt="bj" />
    </div>
    <register ref="register"></register>
  </div>
</template>

<script>
import register from "@/views/register";
import { loginPost } from "@/api/login.js";
import {setLocal, getLocal} from "@/utils/local.js"
export default {
  components: {
    register,
  },
  data() {
    const validate = (rule, value, callback, reg, reqMsg, regMsg) => {
      if (value === "") return callback(new Error(reqMsg));
      reg.test(value) ? callback() : callback(new Error(regMsg));
    };
    const validatePhone = (rule, value, callback) => {
      const _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
      validate(
        rule,
        value,
        callback,
        _reg,
        "请输入手机号",
        "请输入正确的手机号"
      );
    };
    const validatePass = (rule, value, callback) => {
      // const _reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/;
      const _reg = /^\w+$/;

      validate(
        rule,
        value,
        callback,
        _reg,
        "请输入密码",
        "请输入字母和数字组成的8到16的密码,不能是纯数值和纯字母"
      );
    };

    return {
      //   基地址
      baseUrl: process.env.VUE_APP_URL,
      imageCodeUrl: process.env.VUE_APP_URL + "/captcha?type=login",
      form: {
        phone: "",
        password: "",
        code: "",
        isPass: [],
      },
      // 验证字段
      rules: {
        phone: [{ validator: validatePhone, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
        code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { min: 4, max: 4, message: "请输入正确的验证码", trigger: "blur" },
        ],
        isPass: [{ required: true, message: "请勾选协议", trigger: "change" }],
      },
    };
  },
  beforeCreate() {
      if (getLocal('token')) {
        this.$router.push('/layout');
      }
    },
  methods: {
    
    updateImgUrl() {
      // this.clearCookie('PHPSESSID')
      this.imageCodeUrl =
        process.env.VUE_APP_URL +
        "/captcha?type=login" +
        "&fdafds=" +
        Date.now();
    },
    login() {
      this.$refs.form.validate((result) => {
        if (result) {
          loginPost(this.form)
            .then((res) => {
              if (res.data.code === 200) {
                this.$message.success("登录成功");
                setLocal('token', res.data.data.token);
                this.$router.push('/layout');
              } else  {
                this.updateImgUrl();
                const filed = (res.data.message === "验证码错误") ? "code" : "password";
                const errMsg = (filed === "code") ?  "验证码错误" : "用户名或密码错误"
                this.rules[filed].push({
                  validator: (rule, value, callback) => {
                    callback(new Error(errMsg));
                  },
                });
                // 触发验证
                this.$refs.form.validateField(filed);
                // 删除临时验证
                this.rules[filed].length = 1;
              }
            })
            .catch((error) => {
              this.$message.error(error);
            });
        }
      });
    },
    // 显示注册界面
    showRegister() {
      this.$refs.register.isShow = true;
    },
  },
};
</script>

<style lang="less">
.login {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);

  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 44px 43px;
    .header {
      .logo {
        width: 25px;
        height: 18px;
        margin-right: 12px;
      }

      span {
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-size: 22px;
        color: #0c0c0c;
        vertical-align: bottom;
      }
      .line {
        display: inline-block;
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin: 0px 12px;
      }
    }
    .form {
      margin-top: 31px;
      .img-code {
        height: 40px;
        width: 100%;
        vertical-align: top;
      }
      .pass-protocol {
        color: #999;
      }
      .btn {
        width: 100%;
      }
    }
  }
}
</style>